package com.macro.mall.es.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.es.dto.EsIndexDto;
import com.macro.mall.es.service.EsIndexService;
import org.elasticsearch.client.RestHighLevelClient;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/index")
public class EsIndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsIndexController.class);

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private EsIndexService esIndexService;

    /**
     * 创建索引接口
     * @return
     */
    @PostMapping("/createIndex")
    public CommonResult createIndex(@RequestBody EsIndexDto esIndexDto) {
        esIndexService.createIndex(esIndexDto);
        return CommonResult.success("索引创建成功");
    }

    /**
     * 检查索引是否存在
     */
    @GetMapping("/indexExists/{indexName}")
    public CommonResult indexExists(@PathVariable String indexName) {
        boolean b = esIndexService.indexExists(indexName);
        return CommonResult.success(b);
    }

    /**
     * 删除索引接口
     */
    @DeleteMapping("/deleteIndex/{indexName}")
    public CommonResult deleteIndex(@PathVariable String indexName) {
        esIndexService.deleteIndex(indexName);
        return CommonResult.success("索引删除成功");
    }

    /**
     * 获取索引接口
     */
    @GetMapping("/getIndex/{indexName}")
    public CommonResult<Map<String,Object>> getIndex(@PathVariable String indexName) {
        Map<String, Object> indexMappings = esIndexService.getIndex(indexName);
        return CommonResult.success(indexMappings);
    }

    /**
     * 根据索引名称修改索引位置
     */
    @PutMapping("/updateIndex")
    public CommonResult updateIndex(@RequestBody EsIndexDto esIndexDto) {
        esIndexService.updateIndex(esIndexDto);
        return CommonResult.success("索引更新成功");
    }

    /**
     * 搜索查询
     * @param esIndexDto
     * @return
     */
    @PostMapping("/search")
    public CommonResult search(@RequestBody EsIndexDto esIndexDto) {
        esIndexService.search(esIndexDto);
        return CommonResult.success("成功");
    }

}
